package com.shop.cereshop.commons.utils.kd.baseCity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kuaidi100.sdk.api.BsameCityExpress;
import com.kuaidi100.sdk.contant.ApiInfoConstant;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.PrintReq;
import com.kuaidi100.sdk.request.bsamecity.BsamecityAddfeeReq;
import com.kuaidi100.sdk.request.bsamecity.BsamecityCancelReq;
import com.kuaidi100.sdk.request.bsamecity.BsamecityOrderReq;
import com.kuaidi100.sdk.response.bsamecity.BsamecityCancelResp;
import com.kuaidi100.sdk.response.bsamecity.BsamecityOrderResp;
import com.kuaidi100.sdk.utils.SignUtils;
import com.shop.cereshop.commons.domain.kuaidi.kd100.order.resp.BsamecityOrderBaseResp;
import com.shop.cereshop.commons.domain.kuaidi.kd100.order.resp.baseCity.MyBsamecityPriceResp;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Type;

@Slf4j
public class BaseCityKdUtil {
    /**
     * 同城寄件-预下单
     */
    public static BsamecityOrderBaseResp<MyBsamecityPriceResp> bsamecityPrice(String key, String secret, BsamecityOrderReq bsamecityOrderReq) {
        PrintReq printReq = new PrintReq();
        String t = System.currentTimeMillis() + "";
        String param = new Gson().toJson(bsamecityOrderReq);
        printReq.setKey(key);
        printReq.setMethod(ApiInfoConstant.PRICE);
        printReq.setT(t);
        printReq.setSign(SignUtils.printSign(param, t, key, secret));
        printReq.setParam(param);
        try {
            IBaseClient bsamecityPrice = new BsameCityExpress();
            HttpResult execute = bsamecityPrice.execute(printReq);
            System.out.println(bsamecityPrice.execute(printReq));
            if (execute.getStatus() == 200) {
                // 使用TypeToken解决泛型类型擦除问题
                Type type = new TypeToken<BsamecityOrderBaseResp<MyBsamecityPriceResp>>() {
                }.getType();
                BsamecityOrderBaseResp<MyBsamecityPriceResp> printBaseResp = new Gson().fromJson(execute.getBody(), type);
                return printBaseResp;
            }
        } catch (Exception e) {
            log.error("同城预下单失败 {} ", e.getMessage());
        }
        BsamecityOrderBaseResp printBaseResp = new BsamecityOrderBaseResp();
        printBaseResp.setSuccess(false);
        printBaseResp.setMessage("系统内部错误");
        printBaseResp.setCode(505);
        return printBaseResp;
    }

    /**
     * 同城寄件-下单
     */
    public static BsamecityOrderBaseResp<BsamecityOrderResp> bsamecityOrder(String key, String secret, BsamecityOrderReq bsamecityOrderReq) {
        PrintReq printReq = new PrintReq();
        String t = System.currentTimeMillis() + "";
        String param = new Gson().toJson(bsamecityOrderReq);
        printReq.setKey(key);
        printReq.setMethod(ApiInfoConstant.BSAMECITY_ORDER);
        printReq.setT(t);
        printReq.setSign(SignUtils.printSign(param, t, key, secret));
        printReq.setParam(param);
        try {
            IBaseClient bsamecityPrice = new BsameCityExpress();
            HttpResult execute = bsamecityPrice.execute(printReq);
            log.info("同城下单返回参数 execute:{}", execute);
            if (execute.getStatus() == 200) {
                // 使用TypeToken解决泛型类型擦除问题
                Type type = new TypeToken<BsamecityOrderBaseResp<BsamecityOrderResp>>() {
                }.getType();
                BsamecityOrderBaseResp<BsamecityOrderResp> printBaseResp = new Gson().fromJson(execute.getBody(), type);
                return printBaseResp;
            }
        } catch (Exception e) {
            log.error("同城下单失败 {} ", e.getMessage());
        }
        BsamecityOrderBaseResp printBaseResp = new BsamecityOrderBaseResp();
        printBaseResp.setSuccess(false);
        printBaseResp.setMessage("系统内部错误");
        printBaseResp.setCode(505);
        return printBaseResp;
    }

    /**
     * 同城寄件-预取消
     */
    public static BsamecityOrderBaseResp<BsamecityCancelResp> bsamecityPrecancel(String key, String secret, BsamecityCancelReq bsamecityCancelReq) {
        PrintReq printReq = new PrintReq();
        String t = System.currentTimeMillis() + "";
        String param = new Gson().toJson(bsamecityCancelReq);

        printReq.setKey(key);
        printReq.setMethod(ApiInfoConstant.BSAMECITY_PRECANCEL);
        printReq.setT(t);
        printReq.setSign(SignUtils.printSign(param, t, key, secret));
        printReq.setParam(param);
        try {
            IBaseClient bsamecityPrice = new BsameCityExpress();
            HttpResult execute = bsamecityPrice.execute(printReq);
            if (execute.getStatus() == 200) {
                // 使用TypeToken解决泛型类型擦除问题
                Type type = new TypeToken<BsamecityOrderBaseResp<BsamecityCancelResp>>() {
                }.getType();
                BsamecityOrderBaseResp<BsamecityCancelResp> printBaseResp = new Gson().fromJson(execute.getBody(), type);
                return printBaseResp;
            }
        } catch (Exception e) {
            log.error("同城寄件-预取消失败 {}", e.getMessage());
        }
        BsamecityOrderBaseResp printBaseResp = new BsamecityOrderBaseResp();
        printBaseResp.setSuccess(false);
        printBaseResp.setMessage("系统内部错误");
        printBaseResp.setCode(505);
        return printBaseResp;
    }

    /**
     * 同城寄件-取消
     */
    public static BsamecityOrderBaseResp<BsamecityCancelResp> bsamecityCancel(String key, String secret, BsamecityCancelReq bsamecityCancelReq) {
        PrintReq printReq = new PrintReq();

        String t = System.currentTimeMillis() + "";
        String param = new Gson().toJson(bsamecityCancelReq);

        printReq.setKey(key);
        printReq.setMethod(ApiInfoConstant.BSAMECITY_CANCEL);
        printReq.setT(t);
        printReq.setSign(SignUtils.printSign(param, t, key, secret));
        printReq.setParam(param);
        try {
            IBaseClient bsamecityPrice = new BsameCityExpress();
            HttpResult execute = bsamecityPrice.execute(printReq);
            if (execute.getStatus() == 200) {
                BsamecityOrderBaseResp<BsamecityCancelResp> printBaseResp = new Gson().fromJson(execute.getBody(), BsamecityOrderBaseResp.class);
                return printBaseResp;
            }
        } catch (Exception e) {
            log.error("同城寄件-取消  {}", e.getMessage());
        }
        BsamecityOrderBaseResp printBaseResp = new BsamecityOrderBaseResp();
        printBaseResp.setSuccess(false);
        printBaseResp.setMessage("系统内部错误");
        printBaseResp.setCode(505);
        return printBaseResp;
    }

    /**
     * 同城寄件-加小费
     */
    public static void bsamecityAddfee(String key, String secret, BsamecityAddfeeReq bsamecityAddfeeReq) {
        PrintReq printReq = new PrintReq();

        String t = System.currentTimeMillis() + "";
        String param = new Gson().toJson(bsamecityAddfeeReq);

        printReq.setKey(key);
        printReq.setMethod(ApiInfoConstant.BSAMECITY_ADDFEE);
        printReq.setT(t);
        printReq.setSign(SignUtils.printSign(param, t, key, secret));
        printReq.setParam(param);
        try {
            IBaseClient bsamecityPrice = new BsameCityExpress();
            System.out.println(bsamecityPrice.execute(printReq));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
